package cn.com.czpb.client.module.newspager.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;

import butterknife.OnClick;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.MToolBarActivity;
import cn.com.czpb.client.module.dialog.ShareDialog;
import cn.com.czpb.client.utils.FjsonUtil;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;

import butterknife.BindView;

/**
 * Created by 猪猪侠 on 2017/10/30.
 */

public class NewspagerActivity extends MToolBarActivity {
    @BindView(R.id.webview_news_szb)
    WebView mWebView;
    @BindView(R.id.img_activity_back)
    ImageView img_activity_back;
    @BindView(R.id.img_web_back)
    ImageView img_web_back;
    @BindView(R.id.img_share_newspaper)
    ImageView img_share_newspaper;
    private String url;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_newspager_detail;
    }
    @Override
    public void initView() {

        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        WebSettings webSettings = mWebView.getSettings();
        String ua = webSettings.getUserAgentString();
        LogUtils.i("ua-->"+ua);
        webSettings.setUserAgentString(ua+"; gxrbapp/");
        String ua2 = webSettings.getUserAgentString();
        LogUtils.i("ua-->"+ua2);
        webSettings.setSavePassword(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setAppCacheEnabled(true);
        mWebView.addJavascriptInterface(this,"szbListner");

        mWebView.setVerticalScrollBarEnabled(false); //垂直不显示
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()&&mWebView.getUrl().contains("date")){
            mWebView.goBack();
        }else{
            finish();
        }

    }

    @OnClick({R.id.img_activity_back,R.id.img_web_back,R.id.img_share_newspaper})
    public void dobackorshare(View view){
        switch (view.getId()){
            case R.id.img_activity_back:
                finish();
                break;
            case R.id.img_web_back:
                onBackPressed();
                break;
            case R.id.img_share_newspaper:
//                Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + r.getResourcePackageName(R.drawable.redclouds) + "/" + r.getResourceTypeName(R.drawable.redclouds) + "/" + r.getResourceEntryName(R.drawable.redclouds));

                ShareDialog dialog = new ShareDialog(activity, R.style.Sharedialog,mWebView.getTitle(),mWebView.getUrl(),getResources().getString(R.string.shared_img),1);

                dialog.show();
                break;
        }
    }



    @Override
    public void initData() {
//        mWebView.loadUrl("http://nj.99cms.com/demo//web/GJSB_SZB/2016-12/08/7875.html");
        mWebView.loadUrl(url);
    }

    @JavascriptInterface
    public void getInfo(String info) {
        LogUtils.e("getInfo-->"+info);
        JSONObject object = FjsonUtil.parseObject(info);
        String title = object.getString("title");
        String url = object.getString("url");
        LogUtils.e("title-->"+title);
        LogUtils.e("url-->"+url);
        PageCtrl.start2NewsPagerDetailActivity(activity,url,title);

    }
}
